﻿using System;
using System.Xml;
using System.IO;

using ns_FilesManage;

namespace ns_MsgRecords
{
    public class c_MsgRecordsXml
    {
        //存储格式<MSGS_LIST><MSG><FROM_NAME>NAME(发送方)</FROM_NAME><TO_NAME>NAME(接收方)</TO_NAME><DATE_TIME>time发送时间</DATE_TIME><FROM_IP>发送方ip地址</FROM_IP><MSG_CONTENT>文本内容</MSG_CONTENT></MSG></MSGS_LIST>
        #region//类属性
        //标记名称
        private const String MSG = "Msg";
        private const String FROM_NAME = "FromName";
        private const String TO_NAME= "ToName";
        private const String  DATE_TIME = "DateTime";
        private const String FROM_IP = "FromIP";
        private const String MSG_CONTENT = "MsgContent";
        private const String MSGS_LIST = "MsgsList";
        //xml对象，很重要，用于各种与xml有关的操作
        private XmlDocument _XmlDoc = new XmlDocument();

        public String MsgRecordsXml
        {
            get { return _XmlDoc.OuterXml; }
        }
        #endregion
        #region      //构造函数
        public c_MsgRecordsXml()//建立格式头
        {
            XmlDeclaration dec = _XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            _XmlDoc.PrependChild(dec);
            XmlElement ele = _XmlDoc.CreateElement(MSGS_LIST);
            ele.IsEmpty = false;
            _XmlDoc.AppendChild(ele);
        }
        public c_MsgRecordsXml(String MsgRecordsXmlPath)//load的中文问题搞死我了，所以用loadxml
        {
            _XmlDoc.LoadXml(s_c_FilesManage.FilesManage.ReadFile(MsgRecordsXmlPath,true));
        }
        public c_MsgRecordsXml(String MsgRecordsXmlString,Boolean IsString)//区分由路径创建
        {
            _XmlDoc.LoadXml(MsgRecordsXmlString);
        }
        #endregion
        #region       //方法
        public Boolean SaveMsgRecordsXml(String Path)
        {
            try
            {
                s_c_FilesManage.FilesManage.OverWriteFile(Path, _XmlDoc.OuterXml);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }
        public Boolean AddMsgRecord(String FromName , String ToName, String FromIP , String DateTime, String MsgContent)
        {
            try 
            {
                //
                XmlElement ele_FromName = _XmlDoc.CreateElement(FROM_NAME);
                ele_FromName.IsEmpty = false;
                ele_FromName.InnerXml = FromName;

                //
                XmlElement ele_ToName = _XmlDoc.CreateElement(TO_NAME);
                ele_ToName.IsEmpty = false;
                ele_ToName.InnerXml = ToName;
                //
                XmlElement ele_FromIP = _XmlDoc.CreateElement(FROM_IP);
                ele_FromIP.IsEmpty = false;
                ele_FromIP.InnerXml = FromIP;
                //
                XmlElement ele_DateTime = _XmlDoc.CreateElement(DATE_TIME);
                ele_DateTime.IsEmpty = false;
                ele_DateTime.InnerXml = DateTime;
                //
                XmlElement ele_MsgContent = _XmlDoc.CreateElement(MSG_CONTENT);
                ele_MsgContent.IsEmpty = false;
                ele_MsgContent.InnerXml = MsgContent;

                //
                XmlElement ele_Msg = _XmlDoc.CreateElement(MSG);
                ele_Msg.IsEmpty = false;
                ele_Msg.AppendChild(ele_FromName);
                ele_Msg.AppendChild(ele_ToName);
                ele_Msg.AppendChild(ele_FromIP);
                ele_Msg.AppendChild(ele_DateTime);
                ele_Msg.AppendChild(ele_MsgContent);

                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                if (node_MsgsList == null) return false;
                node_MsgsList.AppendChild(ele_Msg);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public String AddMsgRecords(c_MsgRecordsXml SrcRecord)//将SrcRecord的记录添加进来,生成的新的xml文件以字符串的形式返回，但没有真正将this的内容改变
        {//由于文件可能很多，记录很多，如果一条一条加进来工作量大，所以用一种投机的方法
            String temp = MsgRecordsXml;
            String src = SrcRecord.MsgRecordsXml;
            src = src.Substring(65, src.Length-76);
            temp = temp.Substring(0, temp.Length - 11) + src + temp.Substring(temp.Length - 11, 11);
            return temp;
        }
        public Boolean AddMsgRecord(XmlNode MsgNode)
        {
            try
            {
                XmlNode temp = _XmlDoc.ImportNode(MsgNode, true );
                _XmlDoc.ChildNodes.Item(1).AppendChild(temp);
                return true;
            }
            catch (Exception e)
            { 
                return false;
            }
            
        }
        public Boolean DelMsgRecord(XmlNode MsgNode)
        {
            try
            {
                XmlNode temp = _XmlDoc.ImportNode(MsgNode, true);
                _XmlDoc.ChildNodes.Item(1).RemoveChild(temp);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }
        public String GetAllRecordsXml()//返回值是文件全部内容，包括文件声明
        {
            return MsgRecordsXml;
        }
        public String GetRecordsXmlByFromName(String FromName)
        {
            c_MsgRecordsXml tempMsgRecordsXml = new c_MsgRecordsXml();
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                XmlNode node_tempMsgsList = tempMsgRecordsXml._XmlDoc.SelectSingleNode(MSGS_LIST);
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (ele_ChildNodes.Name == FROM_NAME && ele_ChildNodes.InnerXml == FromName)
                        {
                            String from = null, to = null, ip = null, date = null, content = null;
                            for (int ii = 0; ele_Msg.ChildNodes.Item(ii) != null; ii++)
                            {
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_NAME) from = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == TO_NAME) to = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_IP) ip = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == DATE_TIME) date = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == MSG_CONTENT) content = ele_Msg.ChildNodes.Item(ii).InnerXml;
                            }
                            if (from != null && to != null && ip != null && date != null && content != null)
                            {
                                tempMsgRecordsXml.AddMsgRecord(from, to, ip, date, content);
                                break;//不必检查Msg的其他项
                            }
                        }
                    }
                }
                if (node_tempMsgsList.FirstChild == null) return null;
                else return tempMsgRecordsXml.MsgRecordsXml;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public String GetRecordsXmlByToName(String ToName)
        {
            c_MsgRecordsXml tempMsgRecordsXml = new c_MsgRecordsXml();//克隆符合条件的信息
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                XmlNode node_tempMsgsList = tempMsgRecordsXml._XmlDoc.SelectSingleNode(MSGS_LIST);
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (ele_ChildNodes.Name == TO_NAME && ele_ChildNodes.InnerXml == ToName)
                        {
                            String from = null, to = null, ip = null, date = null, content = null;
                            for (int ii = 0; ele_Msg.ChildNodes.Item(ii) != null; ii++)
                            {
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_NAME) from = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == TO_NAME) to = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_IP) ip = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == DATE_TIME) date = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == MSG_CONTENT) content = ele_Msg.ChildNodes.Item(ii).InnerXml;
                            }
                            if (from != null && to != null && ip != null && date != null && content != null)
                            {
                                tempMsgRecordsXml.AddMsgRecord(from, to, ip, date, content);
                                break;//不必检查Msg的其他项
                            }
                        }
                    }
                }
                if (node_tempMsgsList.FirstChild == null) return null;
                else return tempMsgRecordsXml.MsgRecordsXml;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public String GetRecordsXmlByDateTime(DateTime Time)
        {
            c_MsgRecordsXml tempMsgRecordsXml = new c_MsgRecordsXml();//克隆符合条件的信息
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                XmlNode node_tempMsgsList = tempMsgRecordsXml._XmlDoc.SelectSingleNode(MSGS_LIST);
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (ele_ChildNodes.Name == DATE_TIME && Convert.ToDateTime(ele_ChildNodes.InnerXml) == Time)
                        {//符合条件，复制信息
                            String from = null, to = null, ip = null, date = null, content = null;
                            for (int ii = 0; ele_Msg.ChildNodes.Item(ii) != null; ii++)
                            {
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_NAME) from = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == TO_NAME) to = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_IP) ip = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == DATE_TIME) date = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == MSG_CONTENT) content = ele_Msg.ChildNodes.Item(ii).InnerXml;
                            }
                            if (from != null && to != null && ip != null && date != null && content != null)
                            {
                                tempMsgRecordsXml.AddMsgRecord(from, to, ip, date, content);
                                break;//不必检查Msg的其他项
                            }
                        }
                    }
                }
                if (node_tempMsgsList.FirstChild == null) return null;
                else return tempMsgRecordsXml.MsgRecordsXml;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public String GetRecordsXmlByDateTime(DateTime Begin, DateTime End)
        {
            if (Begin > End) return null;
            c_MsgRecordsXml tempMsgRecordsXml = new c_MsgRecordsXml();//克隆符合条件的信息
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                XmlNode node_tempMsgsList = tempMsgRecordsXml._XmlDoc.SelectSingleNode(MSGS_LIST);
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (ele_ChildNodes.Name == DATE_TIME && Convert.ToDateTime(ele_ChildNodes.InnerXml) > Begin && Convert.ToDateTime(ele_ChildNodes.InnerXml)<End)
                        {
                            String from = null, to = null, ip = null, date = null, content = null;
                            for (int ii = 0; ele_Msg.ChildNodes.Item(ii) != null; ii++)
                            {
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_NAME) from = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == TO_NAME) to = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_IP) ip = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == DATE_TIME) date = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == MSG_CONTENT) content = ele_Msg.ChildNodes.Item(ii).InnerXml;
                            }
                            if (from != null && to != null && ip != null && date != null && content != null)
                            {
                                tempMsgRecordsXml.AddMsgRecord(from, to, ip, date, content);
                                break;//不必检查Msg的其他项
                            }
                        }
                    }
                }
                if (node_tempMsgsList.FirstChild == null) return null;
                else return tempMsgRecordsXml.MsgRecordsXml;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public String GetRecordsXmlByFromIP(String FromIP)
        {
            c_MsgRecordsXml tempMsgRecordsXml = new c_MsgRecordsXml();//克隆符合条件的信息
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                XmlNode node_tempMsgsList = tempMsgRecordsXml._XmlDoc.SelectSingleNode(MSGS_LIST);
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (ele_ChildNodes.Name == FROM_IP && ele_ChildNodes.InnerXml == FromIP)
                        {
                            String from = null , to = null , ip = null , date=null , content=null; 
                            for (int ii = 0; ele_Msg.ChildNodes.Item(ii) != null; ii++)
                            {
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_NAME) from = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == TO_NAME) to = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == FROM_IP) ip = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == DATE_TIME) date = ele_Msg.ChildNodes.Item(ii).InnerXml;
                                if (ele_Msg.ChildNodes.Item(ii).Name == MSG_CONTENT) content = ele_Msg.ChildNodes.Item(ii).InnerXml;
                            }
                            if (from != null && to != null && ip != null && date != null && content != null)
                            {
                                tempMsgRecordsXml.AddMsgRecord(from, to, ip, date, content);
                                break;//不必检查Msg的其他项
                            }
                         }
                    }
                }
                if (node_tempMsgsList.FirstChild == null) return null;
                else return tempMsgRecordsXml.MsgRecordsXml;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        private XmlNode GetMsgNodeByLatestTime()
        {
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                XmlNode msgNode = null;
                DateTime lastTime = DateTime.MaxValue;
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++)
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (ele_ChildNodes.Name == DATE_TIME && Convert.ToDateTime(ele_ChildNodes.InnerXml) <= lastTime)
                        {
                            msgNode = ele_Msg;
                            lastTime = Convert.ToDateTime(ele_ChildNodes.InnerXml);
                        }
                    }
                }
                if (msgNode != null) { node_MsgsList.RemoveChild(msgNode); }
                return msgNode;
            }
            
            catch (Exception e)
            {
                return null;
            }
        }
        public String OrderRecordsXmlByDateTime( )
        {
            c_MsgRecordsXml tempMsgRecordsXml = new c_MsgRecordsXml();
            XmlNode node_tempMsgsList = tempMsgRecordsXml._XmlDoc.SelectSingleNode(MSGS_LIST);
            try
            {
                XmlNode temp ;
                for (temp = GetMsgNodeByLatestTime(); temp != null; temp = GetMsgNodeByLatestTime() ) {
                    tempMsgRecordsXml.AddMsgRecord(temp);
                }
                return tempMsgRecordsXml.MsgRecordsXml;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public Boolean IsMsgRecordExist(String FromName, String ToName, String FromIP, String DateTime, String MsgContent)
        {
            try
            {
                XmlNode node_MsgsList = _XmlDoc.SelectSingleNode(MSGS_LIST);
                Int32 sum = 0;
                for (int index = 0; node_MsgsList.ChildNodes.Item(index) != null; index++  )
                {
                    XmlElement ele_Msg = (XmlElement)node_MsgsList.ChildNodes.Item(index);
                    for (int i = 0; ele_Msg.ChildNodes.Item(i) != null; i++)
                    {
                        XmlElement ele_ChildNodes = (XmlElement)ele_Msg.ChildNodes.Item(i);
                        if (
                            ele_ChildNodes.Name == FROM_NAME && ele_ChildNodes.InnerXml == FromName
                            || ele_ChildNodes.Name == TO_NAME && ele_ChildNodes.InnerXml == ToName
                            || ele_ChildNodes.Name == FROM_IP && ele_ChildNodes.InnerXml == FromIP
                            || ele_ChildNodes.Name == DATE_TIME && ele_ChildNodes.InnerXml == DateTime
                            || ele_ChildNodes.Name == MSG_CONTENT && ele_ChildNodes.InnerXml == MsgContent
                            )
                        {
                            sum++;
                        }

                    }
                    if (sum == 5)
                    {
                        return true;
                    }
                    else
                    {
                        sum = 0;
                    }
                   
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        #endregion
    }
}